package com.yuan.yblog.portal.pojo.VO;

import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.crud.entity.User;
import com.yuan.yblog.crud.entity.UserCollection;
import com.yuan.yblog.portal.pojo.contants.Status;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Date;


/**
 *  展示在用户中心的收藏列表
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "用户收藏表项")
public class UserCollectionVO extends UserCollection implements Serializable {

    @Schema(description = "文章作者名")
    private String postUserName;

    @Schema(description = "文章标题")
    private String title;

    @Schema(description = "文章阅读数")
    private Integer viewCount;

    @Schema(description = "文章评论数")
    private Integer commentCount;

    /**
     * 转化为VO，用于传递给前端
     */
    public static UserCollectionVO getUserCollectionVO(UserCollection userCollection, Post post, User user) {
        UserCollectionVO vo = new UserCollectionVO();
        if (post != null) {
            vo.setTitle(post.getTitle());
            vo.setViewCount(post.getViewCount());
            vo.setCommentCount(post.getCommentCount());
        }
        if (user != null) {
            vo.setPostUserName(user.getUsername());
        }
        if (userCollection != null) {
            vo.setId(userCollection.getId());
            vo.setUserId(userCollection.getUserId());
            vo.setPostId(userCollection.getPostId());
            vo.setPostUserId(userCollection.getPostUserId());
            vo.setCreated(userCollection.getCreated());
            vo.setModified(userCollection.getModified());
        }
        return vo;
    }


    public UserCollection toUpdateEntity() {
        UserCollection entity = new UserCollectionVO();
        entity.setId(this.getId());
        entity.setModified(new Date());
        entity.setStatus(this.getStatus());
        return entity;
    }

    public UserCollection toInsertEntity() {
        UserCollection entity = new UserCollectionVO();
        entity.setId(null);
        entity.setUserId(this.getUserId());
        entity.setPostUserId(this.getPostUserId());
        entity.setPostId(this.getPostId());
        entity.setCreated(new Date());
        entity.setModified(new Date());
        entity.setStatus(Status.valid);
        return entity;
    }

}
